<template>
  <!-- 黑名单 -->
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idnum">
        <el-input
          v-model="queryParams.idnum"
          placeholder="请输入身份证号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="黑名单类型" prop="btype">
        <el-input
          v-model="queryParams.btype"
          placeholder="请输入黑名单类型"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="上传人" prop="bloc">
        <el-input
          v-model="queryParams.bloc"
          placeholder="请输入上传人"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"

        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"

        >删除
        </el-button>
      </el-col>

    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="50" align="center"/>
      <el-table-column label="姓名" align="center" key="name" prop="name"/>
      <el-table-column label="身份号" align="center" key="idnum" prop="idnum"/>
      <el-table-column label="黑名单类型" align="center" key="btype" prop="btype"/>
      <el-table-column label="欠费金额" align="center" key="money" prop="money"/>

      <el-table-column label="上传人" align="center" key="bloc" prop="bloc"/>

      <el-table-column label="时间" align="center" key="creatdate" prop="creatdate"/>
      <el-table-column label="是否生效" align="center" key="del" prop="del">
        <template slot-scope="scope">

          <div v-if="scope.row.del=='1'">未生效</div>
          <div v-if="scope.row.del=='0'">生效</div>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" key="remak" prop="remak"/>
      <el-table-column
        label="操作"
        align="center"
        width="160"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.btype =='袭医史'">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"

          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"

          >删除
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">


        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" maxlength="30"/>
        </el-form-item>
        <el-form-item label="身份证号" prop="idnum">
          <el-input v-model="form.idnum" placeholder="请输入身份证号" maxlength="30"/>
        </el-form-item>
        <!--        <el-form-item label="是否袭医" prop="field103">-->
        <!--        <el-checkbox-group v-model="formData.field103" size="small">-->
        <!--          <el-checkbox-button v-for="(item, index) in field103Options" :key="index" :label="item.value"-->
        <!--            :disabled="item.disabled">{{item.label}}</el-checkbox-button>-->
        <!--        </el-checkbox-group>-->
        <!--      </el-form-item>-->


        <el-form-item label="上传人" prop="bloc">
          <el-input v-model="form.bloc" placeholder="请输入上传人" maxlength="30"/>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remak" type="textarea" placeholder="请输入备注" maxlength="120"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import { searchList, delUser, addUser, updateUser } from '@/api/credit/blackList'

export default {
  components: {},
  name: 'patient',
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,

      // 总条数
      total: 0,
      // 用户表格数据
      userList: null,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined,
        idnum: undefined,
        btype: '袭医史',
        bloc: undefined
      },

      // 表单校验
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 20, message: '姓名长度必须介于 2 和 20 之间', trigger: 'blur' }
        ],

        idnum: [
          { required: true, message: '请输入身份证号', trigger: 'blur' }
        ],

        bloc: [
          { required: true, message: '请输入上传人', trigger: 'blur' }
        ]

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true
      searchList(this.queryParams).then(response => {
          this.userList = response.rows
          this.total = response.total
          this.loading = false
        }
      )
    },

    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        idnum: undefined,

        bloc: undefined,
        remak: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置搜索条件按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加用户'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.name = row.name
      this.form.idnum = row.idnum

      this.form.bloc = row.bloc
      this.form.remak = row.remak

      this.form.id = row.id
      this.open = true
      this.title = '修改用户'
    },
    /** 提交按钮 */
    submitForm: function() {
      this.form.btype = '袭医史'
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.userId != undefined) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.id || this.ids
      console.log('userIds', userIds)
      this.$modal.confirm('是否确认删除用户编号为"' + userIds + '"的数据项？').then(function() {
        return delUser({ ids: userIds.toString() })
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    }

  }
}
</script>
